﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class sale_lookup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        string product_name = this.Request.Params["q"].ToString();
        string name = Session["USERNAME"].ToString();
        bool check = true;
        //在INFO_PRODUCT表中查询产品ID，若查询得到的是NULL则不存在若存在则继续
        string product_id = "";
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand com_check = new SqlCommand();
        com_check.Connection = sc;
        com_check.CommandText = "select PRODUCT_ID from INFO_PRODUCT where PRODUCT_NAME=@name";
        com_check.Parameters.AddWithValue("@name",product_name);
        object a = com_check.ExecuteScalar();
        if (a == null)
            check = false;
        else product_id = a.ToString();

        if (check==true)
        {
            //在PRODUCT_SALE表中查询product_id 通过entername得到enterid然后在sale表中查到这个企业的指定产品的销售记录
            SqlCommand com_fuck = new SqlCommand();
            com_fuck.Connection = sc;
            com_fuck.CommandText = "select ENTER_NAME from INFO_ENTER where ENTER_ID=(select ENTER_ID from INFO_PRODUCT where PRODUCT_ID=@name)";
            com_fuck.Parameters.AddWithValue("@name",product_id);
            if (com_fuck.ExecuteScalar().ToString().Equals(Session["USERNAME"].ToString()) == true)
            {
                SqlCommand sc1 = new SqlCommand();
                string sqlStr = "select PRODUCT_SALE.PRODUCT_ID,INFO_PRODUCT.PRODUCT_NAME,INFO_IND.IND_NAME,PRODUCT_SALE.IND_ID,PRODUCT_SALE.SUM,PRODUCT_SALE.SALE_DATE from PRODUCT_SALE,INFO_IND,INFO_PRODUCT where PRODUCT_SALE.PRODUCT_ID=@pid and PRODUCT_SALE.PRODUCT_ID=INFO_PRODUCT.PRODUCT_ID and PRODUCT_SALE.IND_ID=INFO_IND.IND_ID ";
                //以用户名在用户表中获得用户ID在销售记录表中获得消费记录导入
                sc1.CommandText = sqlStr;
                sc1.Connection = sc;
                sc1.Parameters.AddWithValue("@pid", product_id);

                SqlDataReader reader = sc1.ExecuteReader();
                GridView1.DataSource = reader;
                GridView1.DataBind();
                sc.Close();
            }
            else
            {
                sc.Close();
                Page.RegisterStartupScript("ss", "<script>alert('此产品不属于登录用户平台，无法查询有关销售信息！')</script>");

            }
        }
        else
        {
            sc.Close();
            Page.RegisterStartupScript("ss", "<script>alert('产品信息不存在，无法查询到销售记录。')</script>");
        }
    }
}